package com.atguigu.gulimall.seckill.controller;

import com.atguigu.common.utils.R;
import com.atguigu.gulimall.seckill.service.SeckillService;
import com.atguigu.gulimall.seckill.to.SeckillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Classname SeckillController
 * @Description TODO
 * @Date 2021/10/25 22:06
 * @Author Anonymous
 */
@Controller
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    /**
     * 返回当前时间可以参与的秒杀商品信息
     */
    @ResponseBody
    @RequestMapping(value = "/getCurrentSeckillSkus")
    public R getCurrentSecKillSkus() {
        System.out.println("调用了getCurrentSeckillSkus" + new Date());
        //获取到当前可以参加秒杀商品的信息
        List<SeckillSkuRedisTo> vos = seckillService.getCurrentSecKillSkus();
        return R.ok().setData(vos);
    }

    /**
     * 返回当前商品的秒杀信息
     */
    @ResponseBody
    @RequestMapping(value = "/getSeckillSkuInfo/{skuId}")
    public R getSeckillSkuInfo(@PathVariable("skuId") Long skuId) {
        SeckillSkuRedisTo to = seckillService.getSeckillSkuInfo(skuId);
        return R.ok().setData(to);
    }

    /**
     * 秒杀
     */
    @RequestMapping("/kill")
    public String secKill(@RequestParam("killId") String killId, // session_skuID
                          @RequestParam("key") String key,
                          @RequestParam("num") Integer num, Model model) {
        String orderSn = seckillService.kill(killId, key, num);
        // 1.判断是否登录
        //秒杀成功返回一个订单号即可
        model.addAttribute("orderSn", orderSn);
        return "success";
    }
}
